package ATMproject;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Draw extends JPanel implements ActionListener {
    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon drawButton = Util.getIcon("/atmimage/buttonSure.jpg");

    private JTextField drawText = new JTextField();
    private JButton drawBtn = new JButton(drawButton);
    private JLabel drawJlabel = new JLabel("请输入您的取款金额：");

    public Draw(MainPage frame) {
        this.frame = frame;

        this.setLayout(null);

        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("取款");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        //取款框
        drawText.setBounds(300, 150, 200, 25);
        this.add(drawText);

        //确认键
        drawBtn.setBounds(350, 200, 100, 50);
        this.add(drawBtn);
        drawBtn.addActionListener(this);

        //提示
        drawJlabel.setBounds(170,150,150,25);
        this.add(drawJlabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
            return;
        }
        if (Double.valueOf(drawText.getText()) < frame.getUserarraylist().get(frame.getIndex()).getMoney()) {
            ArrayList<User> user = (ArrayList<User>) Util.readObject("D:/4.dat");
            double drawMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() - Double.valueOf(drawText.getText());
            frame.getUserarraylist().get(frame.getIndex()).setMoney(drawMoney);
            Util.writeObject("D:/4.dat",user);
            drawText.setText("");
            JOptionPane.showMessageDialog(this, "您当前余额为："+drawMoney);
            return;
        } else {
            JOptionPane.showMessageDialog(this, "余额不足");
            drawText.setText("");
        }


    }

}
